package com.iceframework.core.quartz.utils;

import org.quartz.Job;
import org.quartz.SchedulerException;

/**
 * 定时任务工具类型
 * 
 * @author Palo
 *
 */
public class ScheduleJobUtils extends QuartzUtils {

	private final static String TRIGGER_GROUP_PREFIX = "ICE_TRIGGER_GROUP_";// 触发器组Prefix

	/**
	 * 第一种：按秒定时 单位：秒
	 * 
	 * @param jobName
	 * @param triggerName
	 * @param jobClass
	 * @param seconds
	 * @throws SchedulerException
	 */
	public static void addJob(String jobName, String jobGroupName, Class<? extends Job> jobClass, int seconds) throws SchedulerException {
		QuartzUtils.addJob(jobName, jobGroupName, getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName), jobClass, seconds);
	}

	/**
	 * 第一种：按秒定时 单位：秒
	 * 
	 * @param jobName
	 * @param triggerName
	 * @param jobClass
	 * @param seconds
	 * @throws SchedulerException
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static void addJob(String jobName, String jobGroupName, String jobClassName, int seconds) throws SchedulerException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		QuartzUtils.addJob(jobName, jobGroupName, getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName), getJobClass(jobClassName), seconds);
	}

	/**
	 * 第二种：按表达式定时 CronScheduleBuilder.cronSchedule("0 17 1 * * ?")
	 * CronTrigger使用cron表达式来设置触发时间。CronTrigger创建方式：
	 * CronScheduleBuilder.cronSchedule("0 53 19 ? * *") [[秒] [分] [小时] [日] [月] [周]
	 * [年] 当前为每天下午7点53执行
	 * 
	 * @param jobName
	 * @param triggerName
	 * @param jobClass
	 * @param expression
	 * @throws SchedulerException
	 */
	public static void addJob(String jobName, String jobGroupName, Class<? extends Job> jobClass, String expression) throws SchedulerException {
		QuartzUtils.addJob(jobName, jobGroupName, getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName), jobClass, expression);
	}

	/**
	 * 第二种：按表达式定时 CronScheduleBuilder.cronSchedule("0 17 1 * * ?")
	 * CronTrigger使用cron表达式来设置触发时间。CronTrigger创建方式：
	 * CronScheduleBuilder.cronSchedule("0 53 19 ? * *") [[秒] [分] [小时] [日] [月] [周]
	 * [年] 当前为每天下午7点53执行
	 * 
	 * @param jobName
	 * @param triggerName
	 * @param jobClass
	 * @param expression
	 * @throws SchedulerException
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static void addJob(String jobName, String jobGroupName, String jobClassName, String expression) throws SchedulerException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		QuartzUtils.addJob(jobName, jobGroupName, getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName), getJobClass(jobClassName), expression);
	}

	/**
	 * 第一种：按秒定时 单位：秒
	 * 
	 * @param jobName
	 * @param triggerName
	 * @param jobClass
	 * @param seconds
	 * @throws SchedulerException
	 */
	public static void addHttpClientJob(String jobName, String jobGroupName, String url, int seconds) throws SchedulerException {
		QuartzUtils.addHttpClientJob(jobName, jobGroupName, getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName), url, seconds);
	}

	/**
	 * 第二种：按表达式定时 CronScheduleBuilder.cronSchedule("0 17 1 * * ?")
	 * CronTrigger使用cron表达式来设置触发时间。CronTrigger创建方式：
	 * CronScheduleBuilder.cronSchedule("0 53 19 ? * *") [[秒] [分] [小时] [日] [月] [周]
	 * [年] 当前为每天下午7点53执行
	 * 
	 * @param jobName
	 * @param triggerName
	 * @param jobClass
	 * @param expression
	 * @throws SchedulerException
	 */
	public static void addHttpClientJob(String jobName, String jobGroupName, String url, String expression) throws SchedulerException {
		QuartzUtils.addHttpClientJob(jobName, jobGroupName, getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName), url, expression);
	}

	/**
	 * @Description: 修改一个任务的触发时间
	 * 
	 * @param jobName
	 * @param jobGroupName
	 * @param triggerName      触发器名
	 * @param triggerGroupName 触发器组名
	 * @param cron             时间设置，参考quartz说明文档
	 */
	public static void modifyJobTime(String jobName, String jobGroupName, String cron) {
		QuartzUtils.modifyJobTime(getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName), cron);
	}

	public static boolean existJob(String jobName, String jobGroupName) throws SchedulerException {
		return QuartzUtils.existJob(getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName));
	}

	/**
	 * @Description: 移除一个任务
	 * 
	 * @param jobName
	 * @param jobGroupName
	 * @param triggerName
	 * @param triggerGroupName
	 */
	public static void removeJob(String jobName, String jobGroupName) {
		QuartzUtils.removeJob(jobName, jobGroupName, getTriggerName(jobName, jobGroupName), getTriggerGroupName(jobGroupName));
	}

	/**
	 * 获取触发器名称
	 * 
	 * @param jobName
	 * @param jobGroupName
	 * @return
	 */
	private static String getTriggerName(String jobName, String jobGroupName) {
		return jobGroupName + jobName;
	}

	/**
	 * 获取触发器组名称
	 * 
	 * @param jobGroupName
	 * @return
	 */
	private static String getTriggerGroupName(String jobGroupName) {
		return TRIGGER_GROUP_PREFIX + jobGroupName;
	}

	private static Class<? extends Job> getJobClass(String jobClassName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		try {
			Job job = (Job) Class.forName(jobClassName).newInstance();
			return job.getClass();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
}
